<template>
	<view class="list">
		<u-search placeholder="请输入学校名称，支持模糊查找" v-model="searchValue" @custom="search" @search="search"></u-search>
		<view v-if="info.list.length">
			<view v-for="(item,i) in info.list" class="font-26" :key="i" style="padding: 42rpx 20rpx;" @click="choose(item)">
				<rich-text :nodes="item.school_name"></rich-text>
			</view>
		</view>
		<view v-else style="height: 80%;">
			<defaultImg />
		</view>
	</view>

</template>

<script setup lang="ts">
	import defaultImg from '@/components/default-img.vue'
	import {
		reactive, ref
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import user from '../../../api/user'
	let searchValue = ref('')
	let info = reactive({
		list: [],
	})

	const choose = (item) => {
		let name = item.school_name.replace(/(<span[^>]*>)/g, '').replace(/(<\/span>)/g, '').replace(/(\\r\\n)/g, '').trim()
		let pages = getCurrentPages()
		let prevPage = pages[pages.length - 2]; //上一页页面实例
		prevPage.$vm.value = {
			text: name
		}
		uni.navigateBack()
	}
	const search = () => {
		searchSchool()
	}
	const searchSchool = async () => {
		const res = await user.searchSchool({
			keyword: searchValue.value
		})
		res.data.map((item, index) => {
			if (item.school_name.includes(searchValue.value)) {
				item.school_name = join(item.school_name, searchValue.value)
			}
		})
		info.list = res.data
	}
	const join = (str, key) => {
		let reg = new RegExp(`(${key})`, 'gm')
		let replace = '<span style="font-weight: 600;">$1</span>'
		return str.replace(reg, replace)
	}
	onLoad(() => {
		
	})
</script>
<style>
	page {
		height: 100%;
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.list {
		height: 100%;
		padding: 20rpx;
	}
</style>